package com.hbpu.maven.spring.tx.dao;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

@Component
public class AccountDao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void deductBalanceByUserName(String userName, Double amount) {
        String sql = "UPDATE account SET balance = balance - ? WHERE userName = ?";
        int rowsAffected = jdbcTemplate.update(sql, amount, userName);
        if (rowsAffected == 0) {
            throw new RuntimeException("未找到帐户或用户名扣除失败: " + userName);
        } else {
            System.out.println("扣除成功: " + userName);
        }
    }
}
